#10-1
# with open("learnin_python.txt") as file:
#     results = file.read()
#     print(results)
# with open("learnin_python.txt") as file:
#     results = file.readlines()
#     for result in results:
#         print(result)
# with open("learnin_python.txt") as file:
#     results = file.readlines()
# #下面定义learn非常重要
# learn = ""
# for result in results:
#     learn += result
# print(learn)
#10-2
# with open("learnin_python.txt") as file:
#     results = file.readlines()
# message = ""
# for result in results:
#     message = result.replace("python", "C语言")
#     print(message)
#10-3
# user = input("what's your name? ")
# file = "programming.txt"
# with open(file, "w") as file_obj:
#     file_obj.write(user.title() + "\n")
#10-4
# users = ""
# active = True
# while active:
#     users = input("what's your name? ")
#     if users != "no":
#         print("hello " + users.title() + " !\n")
#         filename = "guest.txt"
#         with open(filename, "a") as guestlists:
#             guestlists.write(users.ttile() + " has logged in." + "\n")
#     elif users == "no":
#         print("thank you")
#         break
#10-5
# users = ""
# reasons = ""
# active = True
# while active:
#     users = input("请问您的用户名是？")
#     reasons = input("为什么喜欢编程？")
#     if reasons != "无":
#         file_name = "reasons.txt"
#         with open(file_name, "a") as file_open:
#             file_open.write(users + "的理由是：" + reasons + "\n")
#     elif reasons == "无":
#         print("谢谢参与！")
#         active = False
#10-6/7
# print("请输入两个数字，让我来帮你做加法")
# print("请按'退出'结束")
# while True:
#     first_num = input("第一个数字：")
#     second_num = input("第二个数字：")
#     try:
#         answer = int(first_num) + int(second_num)
#     except ValueError:
#         print("抱歉，您输入的数字有误，请重新输入。")
#     else:
#         print("答案是：" + str(answer))
#     是否退出 = input("是否退出 ")
#     if 是否退出 == "是":
#         break
#10-8/9
# def search(file):
#     try:
#         with open(file) as f:
#             con = f.read()
#     except FileNotFoundError:
#         #print("sorry the " + file + " not found.\n")
#         pass
#     else:
#         print(con)
# files = ["dogs.txt", "cats.txt","alice.txt"]
# for file in files:
#     search(file)
#10-10
# def count(file):
    # with open(file) as f:
        # con = f.read()
# #关于这个查询单词的，有些字母里面包含the，也被查询到了
    # words = con.lower().split().count("the")
    # print(str(words))
# files = ["alice.txt", "as time glide.txt", "france and england.txt"]
# for file in files:
    # count(file)
#10-11/12
# import json
# def remember_num():
#     filename = "favnumber.json"
#     try:
#         with open(filename) as f:
#             fav_num = json.load(f)
#     except FileNotFoundError:
#         return None
#     else:
#         return fav_num
# def get_num():
#     fav_num = remember_num()
#     if fav_num:
#        print("your fav num is: " + str(fav_num))
#     else:
#         fav_num = input("what's your favourite number? ")
#         filename = "favnumber.json"
#         with open(filename,"w") as f:
#             json.dump(fav_num, f)
#         print("ok, i'll remember that.")
# get_num()
# import json
# with open(filename) as f:
    # fav_num = json.load(f)
# print(fav_num)
#10-13
import json
def get_stored_username():
    """如果存储了用户名，获取"""
    filename = "username.json"
    try:
        with open(filename) as f:
            username = json.load(f)
    except FileNotFoundError:
        return None
    else:
        return username
def get_new_username():
    """提示用户输入用户名"""
    username = input("what's your name? ")
    filename = "username.json"
    with open(filename, "w") as f:
        json.dump(username, f)
    return username
def right_or_wrong():
    """询问用户名是否输入正确"""
    question = input("did you input the right name? ")
    if question =="yes":
        username = get_stored_username()
    else:
        username = get_new_username()
def greet_user(name):
    """问候用户，并指出起名字"""
    username = get_stored_username()
    if username:
        print("welcome back, " + username)
    else:
        answer = input("did you input the right name? ")
        if answer == "yes":
            username = get_new_username()
            print("we'll remember you, " + username)
        else:
            print("please input it again.")
right_or_wrong()